﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using QLTB.Data;
using QLTB.Entities;
using _4rumDCT.App_Code;

namespace QuanLyThietBi.DCTEDU_
{
    public partial class CanBoMaster : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string whereClause = "TrangThai=0 and MaCanBo='" + Common.GetKeyUserInfo("Id") + "'";
            int totalRow = 0;
            lblThongBao.Text = DataRepository.HoatDongSuaChuaProvider.GetPaged(whereClause, "", 0, 0, out totalRow).Count.ToString();
            if (!IsPostBack)
            {
                Pager1.CurrentIndex = 1;
                BindHoatDong(Pager1.CurrentIndex);
                Pager2.CurrentIndex = 1;
                BindLienHe(Pager2.CurrentIndex);
                BindTienTrinh();
            }
             if (Request.Cookies["DangNhap"] != null)
                {
                    HttpCookie cookie = Request.Cookies.Get("UserNameAndPassword");
                    if (cookie != null)
                    {
                        CanBo ent_CanBo = DataRepository.CanBoProvider.GetByTenTruyCap(cookie.Values["UserName"]);
                        lblFullName.Text = ent_CanBo.HoTen+ "("+ent_CanBo.MaCanBo+")";
                    }
                }
            
        }

        private void BindTienTrinh()
        {
            int totalRows = 0;
            string whereClause = "MaCanBo = '" + Common.GetKeyUserInfo("Id") + "' and MaThietBi in (select MaThietBi from ThietBi where TrangThai=0) and GetDate()-NgayCapNhat<5";
            TList<HoatDongSuaChua> TList_HoatDong = DataRepository.HoatDongSuaChuaProvider.GetPaged(whereClause,"MaThietBi DESC",0,0, out totalRows);
            rpTiennTrinh.DataSource = TList_HoatDong;
            rpTiennTrinh.DataBind();
            lblTienTrinh.Text = TList_HoatDong.Count().ToString();
        }

        private void BindLienHe(int PageNo)
        {
            int totalRows = 0;
            string whereClause = "MaCanBo = '" + Common.GetKeyUserInfo("Id") + "' and TrangThai=1";
            DataSet dsLienHe = DataRepository.Provider.ExecuteDataSet("LienHe_GetPaged", whereClause, "NgayCapNhat DESC",
                PageNo - 1, Pager2.PageSize);
            
            rpLienHe.DataSource = dsLienHe.Tables[0];
            rpLienHe.DataBind();
            try
            {
                totalRows = Convert.ToInt32(dsLienHe.Tables[1].Rows[0][0]);
                litcountLienHe.Text = totalRows.ToString();
            }
            catch (Exception)
            {
            }
            Pager2.ItemCount = totalRows;
            if (totalRows <= Pager2.PageSize)
                Pager2.Visible = false;
            else
                Pager2.Visible = true;
        }

        protected void Pager2_Command(object sender, CommandEventArgs e)
        {
            int currnetPageIndex = Convert.ToInt32(e.CommandArgument);
            Pager2.CurrentIndex = currnetPageIndex;
            BindLienHe(currnetPageIndex);
        }

        private void BindHoatDong(int PageNo)
        {
            int totalRows = 0;
            string whereClause = "MaCanBo = '" + Common.GetKeyUserInfo("Id") + "'";
            DataSet dsHoatDongSuaChua = DataRepository.Provider.ExecuteDataSet("HoatDongSuaChua_GetPaged", whereClause, "NgayCapNhat DESC",
                PageNo - 1, Pager1.PageSize);

            rpHoatDong.DataSource = dsHoatDongSuaChua.Tables[0];
            rpHoatDong.DataBind();
            try
            {
                totalRows = Convert.ToInt32(dsHoatDongSuaChua.Tables[1].Rows[0][0]);
            }
            catch (Exception)
            {
            }
            Pager1.ItemCount = totalRows;
            if (totalRows <= Pager1.PageSize)
                Pager1.Visible = false;
            else
                Pager1.Visible = true;
        }

        protected void Pager1_Command(object sender, CommandEventArgs e)
        {
            int currnetPageIndex = Convert.ToInt32(e.CommandArgument);
            Pager1.CurrentIndex = currnetPageIndex;
            BindHoatDong(currnetPageIndex);
        }

        protected void lkThoat_Click(object sender, EventArgs e)
        {
            Session.Clear();
            FormsAuthentication.SignOut();
            FormsAuthentication.RedirectToLoginPage();
        }

        public string getTenThietBi(object ob)
        {
            string id = ob.ToString();
            return DataRepository.ThietBiProvider.GetByMaThietBi(Convert.ToInt32(id)).TenThietBi;

        }

        public string getTenPhong(object ob)
        {
            string id = ob.ToString();
            return DataRepository.PhongHocProvider.GetByMaPhong(DataRepository.ThietBiProvider.GetByMaThietBi(Convert.ToInt32(id)).MaPhong).TenPhong;

        }
        public string getTinhTrang(object ob)
        {
            string id = ob.ToString();
            ThietBi ent_ThietBi = DataRepository.ThietBiProvider.GetByMaThietBi(Convert.ToInt32(id));
            if (ent_ThietBi.TrangThai == true)
                return "<html><img src=../../Images/checkk.png /></html>";
            else
            {
                return "<html><img src=../../Images/redx.png /></html>";
            }

        }

        public string getIdTinhTrang(object ob)
        {
            string id = ob.ToString();
            ThietBi ent_ThietBi = DataRepository.ThietBiProvider.GetByMaThietBi(Convert.ToInt32(id));
            if (ent_ThietBi.TrangThai == true)
                return "1";
            else
            {
                return "0";
            }

        }
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["netTiersConnectionString"].ConnectionString);
        protected void cmdThongBao_OnClick(object sender, EventArgs e)
        {
            string query = string.Empty;
            Thread.Sleep(1000);
            
            conn.Open();
            query = "UPDATE HoatDongSuaChua SET TrangThai = 1 where MaCanBo = '" + Common.GetKeyUserInfo("Id") + "'";
            SqlCommand sqlCommand = new SqlCommand();
            sqlCommand.Connection = conn;
            sqlCommand.CommandText = query;
            sqlCommand.ExecuteNonQuery();
            conn.Close();
            BindHoatDong(Pager1.CurrentIndex);
            Page_Load(sender, e);
        }

        protected void cmdTienTrinh_OnClick(object sender, EventArgs e)
        {
            Thread.Sleep(3000);
        }

        public string getDeadline(object ob)
        {
            string ngay = ob.ToString();
            int ts;
            DateTime Fromday = Convert.ToDateTime(ngay);
            DateTime ToDay = Common.GetServerDateTime();
            ts = (ToDay - Fromday).Days;
            return (5-ts).ToString();
        }

        public string getPTDeadline(object ob)
        {
            string ngay = ob.ToString();
            int ts;
            DateTime Fromday = Convert.ToDateTime(ngay);
            DateTime ToDay = Common.GetServerDateTime();
            ts = (ToDay - Fromday).Days;
            switch (5-ts)
            {
                case 1:
                    return "75%";
                case 2:
                    return "50%";
                case 3:
                    return "25%";
                default:
                    return "0%";
            }
        }

        public string getNgayCapNhat(object ob)
        {
            int id = Convert.ToInt32(ob.ToString());
            GiaiDap ent_GiaiDap = DataRepository.GiaiDapProvider.GetByMaLienHe(id);
            if (ent_GiaiDap != null)
            {
                return ent_GiaiDap.NgayCapNhat.ToString("dd/MM/yyyy HH:mm");
            }
            else
            {
                return "";
            }
        }

        public string getNoiDung(object ob)
        {
            int id = Convert.ToInt32(ob.ToString());
            GiaiDap ent_GiaiDap = DataRepository.GiaiDapProvider.GetByMaLienHe(id);
            if (ent_GiaiDap != null)
            {
                return ent_GiaiDap.NoiDung;
            }
            else
            {
                return "";
            }
        }
    }
}